package task;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import bean.Customer;
import bean.GoodDetails;
import javafx.concurrent.Task;

public class XmlTask extends Task<List<Customer>> {

	private Map<String, Customer> selectMap;
	private String skr;
	private String fhr;
	private String spbm;
	
	public XmlTask(Map<String, Customer> selectMap,String skr,String fkr,String spbm) {
		this.selectMap = selectMap;
		this.skr = skr;
		this.fhr = fkr;
		//补零操作
		this.spbm = handleZero(spbm);
	}

	@Override
	protected List<Customer> call() throws Exception {
		List<Customer> error =new ArrayList<Customer>();
		for (String name : selectMap.keySet()) {
			Customer customer = selectMap.get(name);
			//生成xml
			Boolean flag = createXml(customer);
			if(flag){
				error.add(customer);
			}
			continue;
		}
		return error;
	}
	
	/**
	 * 生成xml文件
	 * @param txtContent
	 * @return
	 * @throws IOException 
	 */
	private Boolean createXml(Customer customer){
		Document document = DocumentHelper.createDocument();
		document.setXMLEncoding("UTF-8");
		Element root = document.addElement("Kp");
		/**if(customer.getSh() == null){
			return false;
		}**/
		root.addElement("Version").addText("2.0");
		Element fpxx = root.addElement("Fpxx");
		fpxx.addElement("Zsl").addText("1");
		Element fp = fpxx.addElement("Fpsj").addElement("Fp");
		fp.addElement("Djh").addText(customer.getBillCode());
		fp.addElement("Gfmc").addText(customer.getName());
		fp.addElement("Gfsh").addText(customer.getSh() == null?"":customer.getSh().trim());
		fp.addElement("Gfdzdh").addText(customer.getDzdh()== null?"":customer.getDzdh());
		fp.addElement("Gfyhzh").addText(customer.getYhzh()== null?"":customer.getYhzh());
		fp.addElement("Bz").addText(customer.getBz());
		fp.addElement("Fhr").addText(fhr);
		fp.addElement("Skr").addText(skr);
		fp.addElement("Hsbz").addText("1");
		fp.addElement("Spbmbbh").addText(customer.getSpbmbbh());
		Element spxx = fp.addElement("Spxx").addAttribute("Sph", customer.getSph());
		List<GoodDetails> goodsList = customer.getGoodDetails();
		int step = 0;
		for (GoodDetails goodsDetails : goodsList) {
			Element sph = spxx.addElement("Sph");
			step++;
			sph.addElement("Xh").addText(step+"");
			sph.addElement("Spmc").addText(goodsDetails.getGoodName());
			sph.addElement("Ggxh").addText(goodsDetails.getStandard());
			sph.addElement("Jldw").addText(goodsDetails.getDw());
			sph.addElement("Sl").addText(goodsDetails.getNum());
			sph.addElement("Spbm").addText(spbm);
			sph.addElement("Qyspbm").addText("");
			sph.addElement("Syyhzcbz").addText("0");
			sph.addElement("Lslbz").addText("1");
			sph.addElement("Yhzcsm").addText("");
			sph.addElement("Kce").addText("0");
			sph.addElement("Dj").addText(goodsDetails.getPrice());
			sph.addElement("Slv").addText("0");
			sph.addElement("Je").addText( goodsDetails.getAmount());
		}
		
		String xmlFile = "xml/发票号"+customer.getBillCode()+".xml";
		try {
			File f = new File(xmlFile);
			if(!f.getParentFile().exists()){
				f.getParentFile().mkdirs();
			}
			if (!f.exists()) {
				f.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(xmlFile);  
			OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");  
			OutputFormat of = new OutputFormat();  
			of.setEncoding("UTF-8");  
			of.setIndent(true);  
			of.setIndent("    ");  
			of.setNewlines(true);  
			XMLWriter writer = new XMLWriter(osw, of);  
			writer.write(document);  
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}    
		return true;
	}
	
	/**
	 * 补零操作
	 * @param spbm
	 * @return
	 */
	private String handleZero(String spbm) {
		int temp = 19 - spbm.length();
		for (int i = 0; i < temp; i++) {
			spbm = spbm+"0";
		}
		return spbm;
	}
}
